<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class ScheduleController extends BaseController {

    public function __construct($action, $urlValues) {
        if (!Helper::isLogin()) {
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            header('Location: ' . BASE_URL . 'account/login');
        }
        parent::__construct($action, $urlValues);
        require("models/schedule.php");
        $this->model = new ScheduleModel();
    }

    public function index() {
        $this->view->output($this->model->index());
    }

    public function getDateToDate() {
        if (isset($this->urlValues['id']) && preg_match("/([0-9]{8})?to([0-9]{8})?cls([0-9]{1,})?/", $this->urlValues['id'])) {
            $clsTime = explode("cls", $this->urlValues['id']);
            $spl = explode("to", $clsTime[0]);
            //var_dump($spl);
            $this->view->output($this->model->getDateToDate($spl[0], $spl[1], $clsTime[1]), "empty");
        } else {
            echo "quangthien";
        }
    }

    public function checkExist() {
        if (isset($this->urlValues['id'])) {
            $spl = explode("c", $this->urlValues['id']);
            //var_dump($spl);
            $this->view->output($this->model->checkExist($spl[0], $spl[1]), "empty");
        } else {
            echo "quangthien";
        }
    }

    public function single() {
        if (!empty($_POST)) {
            if ($_POST['subject_id'] == -1 && $_POST['lession_id'] == -1 && empty($_POST['txtDayOff'])) {
                $alert = array(
                    'class' => 'alert-warning',
                    'message' => 'Add reason day-off'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'schedule/single');
            }
            //debuging
            echo "<pre>";
            var_dump($_POST);
            echo "</pre>";
            if ($_POST['subject_id'] == -1 && $_POST['lession_id'] == -1 && !empty($_POST['txtDayOff'])) {
                    $result = $this->model->create($_POST, 2);
            } else {
                if (!empty($_POST['classes_id']) && !empty($_POST['inputScheduleDateSubject']) && !empty($_POST['subject_id']) && !empty($_POST['lession_id'])) {
                    $result = $this->model->create($_POST, 3);
                }
            }

            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Add new schedule successful'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'schedule/index');
                exit();
            }
        }
        //load form create
        $this->view->output($this->model->create(''));
    }

    //create action
    protected function create() {
        //load data post from client
        if (!empty($_POST)) {
            $result = $this->model->create($_POST);
            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Add new schedule successful'
                );
                $_SESSION['message'] = $alert;
                echo $_SESSION['message'];
                header('Location: ' . BASE_URL . 'Schedule/Index');
                exit();
            }
        }
        //load form create
        $this->view->output($this->model->create(''));
    }

    //update action
    protected function update() {
        //load data update post from client
        if (!empty($_POST)) {
            $data = array();
            //get data from POST

            $id = $_POST['schedule_id'];
            $data['dates'] = Helper::convertDate($_POST['dates']);
            $data['lession_id'] = $_POST['lession_id'];

            //update item -> return total rows affected
            $result = $this->model->edit($id, $data);

            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Update lession successful'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'schedule/index');
            }
        }
        //load form update
        $this->view->output($this->model->edit($id = $this->urlValues['id'], $data = ''));
    }

    //delete action
    protected function delete() {
        //when client delete multi item
        if (!empty($_POST)) {
            $result = $this->model->delete('schedule', 'schedule_id IN(' . implode(',', $_POST['id']) . ')');
        } else {
            $result = $this->model->delete('schedule', 'schedule_id=' . $this->urlValues['id']);
        }

        if ($result) {
            $alert = array(
                'class' => 'alert-success',
                'message' => 'Delete schedule successful'
            );
            $_SESSION['message'] = $alert;
        }

        header('Location: ' . BASE_URL . 'schedule/index');
    }

}
